Belgelerim/ceng_491/implementation/doxygen/TraffEdu_Developers_Manual_Reference_ToSource/Path.cpp

Go to the documentation of this file.
00001 
00002 Path::Path(){
00003  numControlPoints = 0;
00004  numPoints = 0;
00005  controlPointsIndex=0;
00006  lineGeode = new osg::Geode();
00007  lineGeom = new osg::Geometry();
00008  dotGeode = new osg::Geode();
00009  dotGeom = new osg::Geometry();
00010   dotGroup= new osg::Group();
00011  lineGroup= new osg::Group();
00012  rotationArrayIndex=0;
00013 }
00014 Path::Path(string pathname,float scale){
00015  numControlPoints = 0;
00016  numPoints = 0;
00017  controlPointsIndex=0;
00018  lineGeode = new osg::Geode();
00019  lineGeom = new osg::Geometry();
00020  dotGeode = new osg::Geode();
00021  dotGeom = new osg::Geometry();
00022  dotGroup= new osg::Group();
00023  lineGroup= new osg::Group();
00024   rotationArrayIndex=0;
00025 
00026 }
00027 
00028 
00029 
00030 osg::Vec3 Path::calculateCatmull(osg::Vec3 A, osg::Vec3 B, osg::Vec3 C,osg::Vec3 D, double t) {
00031         osg::Vec3 P;
00032         P.x() = 0.5 *((2 * B.x()) +
00033                         (-A.x() + C.x()) * t +
00034                         (2*A.x() - 5*B.x() + 4*C.x() - D.x()) * pow(t,2) +
00035                         (-A.x() + 3*B.x() - 3*C.x() + D.x()) * pow(t,3));
00036 
00037         P.y() = 0.5 *((2 * B.y()) +
00038                         (-A.y() + C.y()) * t +
00039                         (2*A.y() - 5*B.y() + 4*C.y() - D.y()) * pow(t,2) +
00040                         (-A.y() + 3*B.y() - 3*C.y() + D.y()) * pow(t,3));
00041 
00042         P.z() = -0.01;
00043 
00044         return P;
00045 }
00046 
00047 void Path::setPoints(double number)
00048 {
00049         if(numControlPoints > 3) {
00050         
00051                 
00052                 osg::Vec3 POld= controlPointArray[controlPointsIndex+1];
00053                 
00054                 
00055                 for(double t = 0.0;t < number ; t += 0.1) {
00056                         
00057                         osg::Vec3 P = calculateCatmull(controlPointArray[controlPointsIndex], controlPointArray[controlPointsIndex+1], controlPointArray[controlPointsIndex+2],controlPointArray[controlPointsIndex+3], t);
00058                         if(controlPointsIndex>0 && t==0.0){
00059                                 
00060                                 continue;
00061                         }
00062                         else{
00063                                 if(controlPointArray[controlPointsIndex+1]==controlPointArray[controlPointsIndex+2]){
00064                                         pointArray[numPoints]=controlPointArray[controlPointsIndex+1];
00065                                         pointArray[numPoints][2]=-0.01;
00066                                         numPoints ++;
00067                                         POld = controlPointArray[controlPointsIndex+1];
00068                                 }
00069                                 else{
00070                                         pointArray[numPoints] = P;
00071                                         numPoints ++;
00072                                         POld = P;
00073                                 }
00074                         }
00075                         
00076                 }
00077                 controlPointsIndex ++;
00078         }
00079                 
00080 }
00081 
00082 
00083 
00084 
00085 osg::Node* Path::displayLines(void)
00086 {
00087         osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array;
00088         color->push_back(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
00089 
00090         osg::ref_ptr<osg::Vec3Array> normal = new osg::Vec3Array;
00091         normal->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
00092         
00093         osg::Vec3Array* vertices = new osg::Vec3Array(numPoints,pointArray);
00094                 
00095 
00096     lineGeom->setVertexArray(vertices);
00097     
00098 
00099     lineGeom->setColorArray(color.get());
00100     lineGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
00101 
00102     lineGeom->setNormalArray(normal.get());
00103     lineGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
00104 
00105         lineGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,0,numPoints));
00106 
00107 
00108         osg::StateSet* stateset = new osg::StateSet;
00109         osg::LineWidth* linewidth = new osg::LineWidth();
00110     linewidth->setWidth(4.0f);
00111     stateset->setAttributeAndModes(linewidth,osg::StateAttribute::ON);
00112     stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
00113     lineGeom->setStateSet(stateset);
00114 
00115     lineGeode->addDrawable(lineGeom);
00116         
00117 
00118         return lineGeode;
00119 }
00120 
00121 osg::Node* Path::displayDots(void)
00122 {
00123         
00124         osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array;
00125         color->push_back(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
00126 
00127         osg::ref_ptr<osg::Vec3Array> normal = new osg::Vec3Array;
00128         normal->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
00129                
00130         osg::Vec3Array* vertices = new osg::Vec3Array(numPoints,pointArray);
00131     dotGeom->setVertexArray(vertices);
00132     
00133     dotGeom->setColorArray(color.get());
00134     dotGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
00135     
00136     dotGeom->setNormalArray(normal.get());
00137     dotGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
00138   
00139   dotGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,numPoints));
00140         dotGeode->addDrawable(dotGeom);
00141                 
00142         osg::StateSet *set = new osg::StateSet();
00143         osg::Point *point = new osg::Point();
00144         float size=3.0;
00145     point->setSize(size);
00146     set->setAttribute(point);
00147         dotGeode->setStateSet(set);
00148         
00149         return dotGeode;
00150 }
00151 
00152 
00153 void Path::addNewPoint( osg::Vec3 pos,double number ) {
00154 
00155         controlPointArray[numControlPoints] = pos;
00156         controlPointArray[numControlPoints][2] += 0.1;
00157         numControlPoints++;
00158         setPoints(number);
00159         displayLines();
00160         displayDots();
00161 }
00162 void Path::draw(osg::ref_ptr<osg::Group> root, int animationFlag){
00163         lineGroup->addChild(lineGeode);
00164         dotGroup->addChild(dotGeode);
00165         objectPAT->addChild(lineGroup);
00166         objectPAT->addChild(dotGroup);
00167         this->addTree(root);
00168 
00169 }
00170 void Path::deletePart(){
00171         controlPointsIndex=0;
00172         numControlPoints=0;
00173         numPoints=0;
00174         rotationArrayIndex=0;   
00175         int numOfChildren=dotGroup->getNumChildren();
00176         for(int i=0;i<numOfChildren;i++)
00177                         dotGroup->removeChild(0,1);
00178         numOfChildren=lineGroup->getNumChildren();
00179         for(int i=0;i<numOfChildren;i++)
00180                         lineGroup->removeChild(0,1);
00181         numControlPoints = 0;
00182 
00183  lineGeode = new osg::Geode();
00184  lineGeom = new osg::Geometry();
00185  dotGeode = new osg::Geode();
00186  dotGeom = new osg::Geometry();
00187   dotGroup= new osg::Group();
00188  lineGroup= new osg::Group();
00189  
00190 
00191 }
00192 osg::Vec3* Path::getControlPointVector(){
00193         return controlPointArray;
00194 }
00195 osg::Vec3* Path::getPointVector(){
00196         return pointArray;
00197 }
00198 double* Path::getRotationArray(){
00199         return rotationArray;
00200 }
00201 void Path::setRotationArray(){
00202         double angle;
00203         int temp=0;
00204         for(int i=0;i<numPoints;++i){
00205                 if(i==0){
00206                         
00207                         osg::Vec3d v1(1.0,0.0,0.0);
00208                         angle=returnAngle(v1,pointArray[i+1]-pointArray[i]);
00209                         rotationArray[i]=angle;
00210                         ++rotationArrayIndex;
00211                 }
00212                 else if(i==numPoints-1){
00213                         osg::Vec3d v2= pointArray[i]-pointArray[i-1];
00214                         osg::Vec3d v1(1.0,0.0,0.0);
00215                         angle=returnAngle(v1,v2);
00216                         rotationArray[i]=angle;
00217                         ++rotationArrayIndex;
00218                 }
00219                 else{
00220                         if(pointArray[i]!=pointArray[i-1]){temp=i-1;}
00221                         osg::Vec3d v1= pointArray[i]-pointArray[i-1];
00222                         osg::Vec3d v2= pointArray[i+1]-pointArray[i];
00223                         osg::Vec3d v3= pointArray[i]-pointArray[temp];
00224                         if(pointArray[i]==pointArray[i-1] && pointArray[i+1]!=pointArray[i]){
00225                                 osg::Vec3d unit(1.0,0.0,0.0);
00226                                 angle=returnAngle(v3,unit);
00227                                 rotationArray[i]=angle;
00228                                 ++rotationArrayIndex;
00229 
00230                                 angle=returnAngle(unit,v2);
00231                                 rotationArray[rotationArrayIndex]=angle;
00232                                 ++rotationArrayIndex;
00233                                                 
00234                         }
00235                         else{
00236                                 angle=returnAngle(v1,v2);
00237                                 rotationArray[rotationArrayIndex]=angle;
00238                                 ++rotationArrayIndex;
00239                         }
00240                         
00241                 }
00242         }
00243 }
00244 double Path::returnAngle(osg::Vec3d v1, osg::Vec3d v2)
00245 {
00246 
00247     double l1 = v1.length();
00248     double l2 = v2.length();
00249     double angle;
00250            
00251         
00252     angle= acos((v1*v2) / (v1.length()*v2.length()));
00253         angle = angle * 180 / M_PI;
00254     
00255         
00256         double dotproduct(v1*v2);
00257         
00258         osg::Vec3 crossproduct(v1^v2);
00259         if(crossproduct[2]>0){
00260                 if((v1[0]==v2[0] && v1[1]==v2[1]&& v1[2]==v2[2]) || dotproduct==v1.length() || dotproduct==v2.length()){
00261                         return 0.0;
00262                 }
00263                 return angle;
00264         }
00265         else{
00266                 if((v1[0]==v2[0] && v1[1]==v2[1]&& v1[2]==v2[2]) || dotproduct==v1.length() || dotproduct==v2.length()){
00267                         return -0.0;
00268                 }
00269         return -angle;
00270         }
00271 }
00272 
00273 
00274 
00275 
00276 
00277 

Generated on Sun May 28 18:54:09 2006 for TraffEdu by  doxygen 1.4.6-NO